<?php

/**
 * Created by JetBrains PhpStorm.
 * User: Grom Hellscream
 * Date: 11/1/12
 * Time: 8:58 PM
 * To change this template use File | Settings | File Templates.
 */
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// import Joomla modelitem library
jimport('joomla.application.component.modelitem');
jimport('joomla.html.pagination');

/**
 * Quanlycongtrinh Model
 */
class VastModelQuanlycongtrinh extends JModelItem {
    public function getCongtrinh()
    {
        $id = JRequest::getInt('id');

        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        
        $query->select('a.*, b.*');
        $query->from('#__hosocanbo AS a');
        $query->join('INNER', '#__congtrinh AS b ON b.hosocanbo_id = a.id');
        
        $query->where('a.id = ' . $id);
        
        $db->setQuery($query);
        return $db->loadObjectList();
    }

    public function deleteItem($congtrinhid)
    {
        $db = JFactory::getDbo();
        
        $query = 'DELETE FROM #__congtrinh WHERE `id` = ' . $congtrinhid;
        
        $db->setQuery($query);
 
        if (!$db->query()) {
            JError::raiseError(500, $db->getErrorMsg());
            return false;
        } else {
            return true;
        }
    }
    
    public function getChiTietCongTrinh()
    {
        $id = JRequest::getInt('congtrinhid');

        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        
        $query->select('a.*');
        $query->from('#__congtrinh AS a');
        
        $query->where('a.id = ' . $id);
        
        $db->setQuery($query);
        return $db->loadObject();
    }
    
    /**
     * Get the tailieus
     * @return string The tailieus to be displayed to the user
     */
    public function getTailieus()
    {
        $id = JRequest::getInt('congtrinhid');

        $db = JFactory::getDBO();
        $query = $db->getQuery(true);

        $query->select('a.*');
        $query->from('#__tailieu AS a');
        $query->where('a.congtrinh_id = ' . $id);

        $db->setQuery($query);
        $tailieus = $db->loadObjectList();
        return $tailieus;
    }  
    
    /**
     * Get the hosocanbo
     * @return string The message to be displayed to the user
     */
    public function getCanbo()
    {
        $id = JRequest::getInt('id');

        $db = JFactory::getDBO();
        $query = $db->getQuery(true);

        $query->select('a.*');
        $query->from('#__hosocanbo AS a');
        $query->where('a.id = ' . $id);

        $db->setQuery($query);
        $canbo = $db->loadObject();
        return $canbo;
    }  
    
    /**
     * Get the message
     * @return string The message to be displayed to the user
     */
    public function getPhong()
    {
        $id = JRequest::getInt('id');

        $db = JFactory::getDBO();
        $query = $db->getQuery(true);

        $query->select('a.*');
        $query->from('#__phong AS a');
        $query->join('INNER', '#__congtac AS b ON b.phong_id = a.id');
        $query->where('b.hosocanbo_id = ' . $id);
        $query->where('b.taivi = 1');
        
        $db->setQuery($query);
        $phong = $db->loadObject();
        
        return $phong;
    }

    public function getKhoi() 
    {
        $db = JFactory::getDBO();
        $query = $db->getQuery(true);
        $donvi = $this->getDonvi();
        $phong = $this->getPhong();
        if ($donvi !== NULL)
        {
            $query->select('a.*');
            $query->from('#__khoi AS a');
            $query->where('a.id = ' . $donvi->khoi_id);
            $db->setQuery($query);
            
            return $db->loadObject();            
        }
        elseif ($phong !== NULL)
        {
            $query->select('a.*');
            $query->from('#__donvi AS a');
            $query->where('a.id = ' . $phong->donvi_id);
            $db->setQuery($query);            
            
            $tmp = $db->loadObject();
            if ($tmp !== NULL)
            {
                $query->select('a.*');
                $query->from('#__khoi AS a');
                $query->where('a.id = ' . $tmp->khoi_id);
                $db->setQuery($query);
                
                return $db->loadObject();                            
            }
        }
        
        return NULL;
    }
        
    public function getDonvi() {
        $id = JRequest::getInt('id');
      
        $db = JFactory::getDBO();
        $query = $db->getQuery(true);

        $query->select('a.*');
        $query->from('#__donvi AS a');
        $query->join('INNER', '#__congtac AS b ON b.donvi_id = a.id');
        $query->where('b.hosocanbo_id = ' . $id);
        $query->where('b.taivi = 1');
        $db->setQuery($query);
        return $db->loadObject();

    }      
}